using System;
using System.Collections.Generic;
using System.Linq;

namespace RestUpMVC.BlogService.Domain
{
    /// <summary>
    /// A Blog domain object following the active record pattern.  Starting out with the data store being an in memory collection.
    /// </summary>
    public class Blog
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public Guid Id { get; private set; }

        private static readonly List<Blog> _blogs = new List<Blog>();

        public static Blog Get(Guid id)
        {
            return _blogs.Where(x => x.Id.Equals(id)).FirstOrDefault();
        }

        public static IEnumerable<Blog> Get()
        {
            return _blogs.ToArray();
        }

        public static void Create(ref Blog blog)
        {
            var newId = Guid.NewGuid();
            while (_blogs.Any(x=>x.Id.Equals(newId)))
            {
                newId = Guid.NewGuid();
            }
            blog.Id = newId;
            _blogs.Add(blog);
        }

        public void Save()
        {
            
        }

        public static void Delete(Blog blog)
        {
            _blogs.Remove(blog);
        }
    }
}